package com.zxy.ziems.server.device.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxy.ziems.server.device.convertor.DeviceConvertor;
import com.zxy.ziems.server.device.mapper.DeviceTypeMapper;
import com.zxy.ziems.server.device.pojo.dto.DeviceTypeDTO;
import com.zxy.ziems.server.device.pojo.entity.DeviceTypeEntity;
import com.zxy.ziems.server.device.pojo.vo.DeviceTypeVO;
import com.zxy.ziems.server.device.service.DeviceTypeService;
import com.zxy.ziems.server.mybatis.core.query.WrapperX;
import com.zxy.ziems.server.mybatis.core.service.ServiceImplX;
import com.zxy.ziems.server.utils.BusinessExceptionUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;

/**
 * 设备类型
 * @author chdzq
 * @create 2023/3/22
 */
@Service
public class DeviceTypeServicImpl extends ServiceImplX<DeviceTypeMapper, DeviceTypeEntity> implements DeviceTypeService {

    @Transactional(
            rollbackFor = Exception.class
    )
    @Override
    public void add(DeviceTypeDTO dto) {
        doCheckBeforeSaveAction(dto);
        DeviceTypeEntity entity = DeviceConvertor.INSTANCE.typeDTOConvert2DO(dto);
        save(entity);
    }

    private void doCheckBeforeSaveAction(DeviceTypeDTO dto) {

        if (StrUtil.isNotBlank(dto.getName())) {
            DeviceTypeEntity old = getByName(dto.getName());
            if (null != old && !Objects.equals(old.getId(), dto.getId())) {
                throw BusinessExceptionUtil.exception("设备类型已存在");
            }
        }
    }

    @Transactional(
            rollbackFor = Exception.class
    )
    @Override
    public void update(DeviceTypeDTO dto) {
        doCheckBeforeSaveAction(dto);
        DeviceTypeEntity entity = DeviceConvertor.INSTANCE.typeDTOConvert2DO(dto);
        updateById(entity);
    }

    @Override
    @Transactional(
            rollbackFor = Exception.class
    )
    public void deleteById(String id) {
        removeById(id);
    }

    private DeviceTypeEntity getByName(String name) {
        return getOne(DeviceTypeEntity::getName, name);
    }

    @Override
    public DeviceTypeEntity getByCode(String code) {
        if (StrUtil.isEmpty(code)) {
            return null;
        }
        return getOne(DeviceTypeEntity::getCode, code);
    }

    @Override
    public List<DeviceTypeEntity> listByIds(Collection<? extends Serializable> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return new ArrayList<>(0);
        }
        return super.listByIds(idList);
    }

    @Override
    public List<DeviceTypeEntity> listByCodes(Collection<? extends Serializable> codes) {
        if (CollectionUtil.isEmpty(codes)) {
            return new ArrayList<>(0);
        }
        return super.list(DeviceTypeEntity::getCode, codes);
    }

    @Override
    public List<DeviceTypeEntity> listAll() {
        LambdaQueryWrapper<DeviceTypeEntity> queryWrapper = WrapperX.<DeviceTypeEntity>lambdaQuery();
        queryWrapper.orderByDesc(DeviceTypeEntity::getSort);
        return list(queryWrapper);
    }

    @Override
    public List<DeviceTypeVO> viewAll() {
        LambdaQueryWrapper<DeviceTypeEntity> queryWrapper = WrapperX.<DeviceTypeEntity>lambdaQuery();
        queryWrapper.orderByDesc(DeviceTypeEntity::getSort);
        List<DeviceTypeEntity> list = list(queryWrapper);
        List<DeviceTypeVO> voList = DeviceConvertor.INSTANCE.typeDOListConvert2VOList(list);
        return voList;
    }

    @Override
    public DeviceTypeEntity loadById(String id) {
        return  baseMapper.selectById(id);
    }
}
